package com.kasax.android.arch.webview.client;

import android.graphics.Bitmap;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kasax.android.arch.utils.LogUtils;
import com.kasax.android.arch.webview.callback.WebViewClientCallback;


public class InternalWebviewClient extends WebViewClient {
    private WebViewClientCallback webViewClientCallback;
    private boolean isLoadError = false;

    public InternalWebviewClient(WebViewClientCallback callbackew) {
        this.webViewClientCallback = callbackew;
    }

    public void setWebViewClientCallback(WebViewClientCallback webViewClientCallback) {
        this.webViewClientCallback = webViewClientCallback;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        LogUtils.d("WebViewClient shouldOverrideUrlLoading");
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        LogUtils.d("WebViewClient onPageStarted");

        if (webViewClientCallback != null) {
            webViewClientCallback.onLoadStart();
        }
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        LogUtils.d("WebViewClient onReceivedError error: " + error.getDescription());
        isLoadError = true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        LogUtils.d("WebViewClient shouldOverrideUrlLoading");
        return super.shouldOverrideUrlLoading(view, request);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        LogUtils.d("WebViewClient onPageFinished");
        webViewClientCallback.onLoadComplete(isLoadError);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
        LogUtils.d("WebViewClient onLoadResource");
    }

    @Override
    public void onPageCommitVisible(WebView view, String url) {
        super.onPageCommitVisible(view, url);
        LogUtils.d("WebViewClient onPageCommitVisible");
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        LogUtils.d("WebViewClient onReceivedError errorCode: " + errorCode + " , description: " + description + " , failingUrl = " + failingUrl);
        isLoadError = true;
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        LogUtils.d("WebViewClient shouldInterceptRequest");
        return super.shouldInterceptRequest(view, request);
    }


}

